<?php
namespace Rainrock\Framework\Controller;

use Rainrock\Framework\kernel\Html\HtmlBase;
use Rainrock\Framework\kernel\Html\HtmlLabel;
use Rainrock\Framework\kernel\Html\HtmlInput;
use Rainrock\Framework\kernel\Html\HtmlButton;
use Rainrock\Framework\kernel\Html\HtmlSelect;
use Rainrock\Framework\kernel\base\Request;
use Rainrock\Framework\kernel\base\Jiami;

/**
*	数据库首页
*/
class DatamanageController extends AuthController{
	
	
	
	/**
	*	数据库管理
	*/
	public function tableAction()
	{
		$alltable = $this->getDb()->getFulltable();
		foreach($alltable as $xu=>$tab){
			$name = $tab['name'];
			if($tab['count'] > 0)$name.='('.$tab['count'].')';
			if($tab['title'])$name .= $tab['title'];
			$data[] = array(
				'value' => $tab['name'],
				'name'	=> $name
			);
		}
		
		$div 	= HtmlLabel::get();
	
		$mdiv 	= HtmlLabel::get()->addCss('panel flex')->addStyle('padding','10px')->setAlign('left');
		$mdiv->addCont(HtmlLabel::get()->addCont('共'.count($alltable).'个表')->addStyle('margin-right','10px'));
		$mdiv->addCont(HtmlSelect::gets()->setData($data)->setId('table_list{rand}')->setWidth(200)->addStyle('margin-right','10px'));
		$likea[] = array('value'=>'like','name'=>'包含');
		$likea[] = array('value'=>'deng','name'=>'等于');
		$likeb[] = array('value'=>'','name'=>'请先选表');
		
		$mdiv->addCont(
			HtmlLabel::get()->addCss('webbtn-group')
			->addCont(HtmlSelect::gets()->setId('fields_select{rand}')->setWidth(150)->setData($likeb))
			->addCont(HtmlSelect::gets()->setValue('deng')->setWidth(100)->setData($likea)->setId('fields_like{rand}'))
			->addCont(HtmlInput::gets()->setWidth(120)->setId('fields_keyword{rand}')->setTishi('输入关键词'))
			->addCont(HtmlButton::gets()->addCont('搜索')->setClickevt('search'))
		);
		
		$mdiv->addCont(
			HtmlLabel::get()->addStyle('flex', 1)
			->setAlign('right')
			->addCont(HtmlButton::gets()->addCont('模块更新')->setClickevt('dbmanage')->addCss('webbtn-default')->setMargin(10,'right'))
			->addCont(HtmlButton::gets()->addCont('字段管理')->addCss('webbtn-default'))
		);
		$div->addCont($mdiv);
		
		$mdiv = HtmlLabel::get()->addCss('panel')->addStyle('margin-top','10px')->setAlign('left');
		$mdiv->addCont(HtmlLabel::get()->addStyle('padding','10px')->setId('table_view{rand}'));
		$div->addCont($mdiv);
		
		return $div->show();
	}
	
	
	public function dataAction()
	{
		$table 	= Request::get('table');
		$sort 	= Request::get('sort');
		$dir  	= Request::get('dir');
		$fshow  = Request::get('fshow');
		$fields_select  	= Request::get('fields_select');
		$fields_like  		= Request::get('fields_like');
		$fields_keyword  	= Jiami::base64decode(Request::get('fields_keyword'));
		$order  = '`id` DESC';
		if($sort && $dir)$order = '`'.$sort.'` '.$dir.'';
		$page 	= Request::getint('page');
		$where  = '1=1';
		
		if($fields_select){
			$where = "`$fields_select`";
			if($fields_like=='like')$where.=" LIKE '%".$fields_keyword."%'";
			if($fields_like=='deng')$where.=" = '".$fields_keyword."'";
		}
		
		
		$barr 	= array();
		$rows 	= $this->getDb()->getall($table, $where,'*', $order, Request::getint('limit'), $page);
		if($this->dbError(true))return $this->returnerror($this->dbError());
		$barr['totalCount'] = $this->nDb->getCount($table, $where);
		$barr['rows'] = $rows;
		
		if($fshow == 'show'){
			$barr['fieldsarr'] = $this->nDb->getAllFields($table);
		}
		return $this->returnsuccess($barr);
	}
}